﻿using System;
using System.Runtime.InteropServices;
using System.Text;

namespace Win32AddIn
{
  public static class WinAPIuser32
  {
    [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
    public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

    [DllImport("User32.dll", EntryPoint = "SendMessage", CharSet = CharSet.Auto)]
    public static extern int SendTextMessage(
        IntPtr hWnd,
        int Msg,
        int wParam,
        string lParam
    );

    [DllImport("user32.dll ", EntryPoint = "GetDlgItem")]
    public static extern IntPtr GetDlgItem( IntPtr hDlg, int nIDDlgItem );

    [DllImport("user32.dll")]
    public static extern int EnumChildWindows(int hWndParent, CallBack lpfn, int lParam);




    /// <summary>
    /// 回调业务
    /// </summary>
    public delegate void CallBusiness(IntPtr hwnd);
    public delegate bool CallBack(IntPtr hwnd, int lParam);
    /// <summary>
    /// 遍历子窗体的父窗体句柄
    /// </summary>
    public static CallBack callBackEnumChildWindows = new CallBack(ChildWindowProcess);
    /// <summary>
    /// 委托业务,需要客户端添加
    /// </summary>
    public static CallBusiness CallFuntion;
    /// <summary>
    /// 遍历子窗体或控件
    /// </summary>
    /// <param name="hWnd"></param>
    /// <param name="lParam"></param>
    /// <returns></returns>
    public static bool EnumChildWindows(IntPtr hWnd, int lParam)
    {
      EnumChildWindows(hWnd.ToInt32(), callBackEnumChildWindows, 0);
      return true;
    }
    /// <summary>
    /// 获取类名字
    /// </summary>
    /// <param name="hwnd">需要获取类名的句柄</param>
    /// <param name="lpClassName">类名(执行完成以后查看)</param>
    /// <param name="nMaxCount">缓冲区</param>
    /// <returns></returns>
    [DllImport("user32.dll", EntryPoint = "GetClassName")]
    public static extern int GetClassName(
        IntPtr hwnd,
        StringBuilder lpClassName,
        int nMaxCount
    );
    /// <summary>
    /// 遍历子控件
    /// </summary>
    /// <param name="hwnd"></param>
    /// <param name="lParam"></param>
    /// <returns></returns>
    public static bool ChildWindowProcess(IntPtr hwnd, int lParam)
    {
      if (CallFuntion != null)
      {
        CallFuntion(hwnd);
      }
      return true;
    }
  }

  /// <summary>
  /// 消息编码
  /// </summary>
  public class MSCODE
  {
    /// <summary>
    /// 鼠标左键按下
    /// </summary>
    public const int WM_LBUTTONDOWN = 0x201;
    /// <summary>
    /// 鼠标左键抬起
    /// </summary>
    public const int WM_LBUTTONUP = 0x202;
    /// <summary>
    /// 设置文本
    /// </summary>
    public const int WM_SETTEXT = 0xC;
  }

}
